package com.sinog.front.controller.biz;

import com.sinog.core.model.UserDTO;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.front.config.log.Log;
import com.sinog.front.feign.biz.BjdxTuoguanInfoService;
import com.sinog.front.service.user.UserFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc 安置帮教管理 - 帮教对象信息管理 - 失联人员管理 SlryglController
 * @author sunsf
 * @date 2020-02-07-13:51
 */
@RestController
public class SlryglController {

    @Resource
    private UserFacade userService;
    @Resource
    private BjdxTuoguanInfoService bjdxTuoguannfoService;

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 失联人员管理 - 列表
     * @param pageIndex 起始页
     * @param pageSize 止页
     * @param request request
     * @param type 是否失联
     * @param sortField 排序字段
     * @param sortOrder 排序方式
     * @return String
     */
    @RequestMapping("/azbj/azbjgl/bjdxInfo/slrygl/list")
    public String getTuoguanList(Integer pageIndex,Integer pageSize,HttpServletRequest request,
                                  @RequestParam(required = false, defaultValue = "-1") String type,
                                 @RequestParam(required = false) String sortField,
                                 @RequestParam(required = false) String sortOrder) {
        String key = request.getParameter("querykey");
        UserDTO user = userService.getAuthentication();
        String departId = user.getDepartid();
        int level = Integer.parseInt(user.getLevel());
        String orgid = user.getOrgid();
        return bjdxTuoguannfoService.getTuoguanList(pageIndex,pageSize,key,departId,level,orgid,type,sortField,sortOrder);
    }

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 失联人员管理 - 失联登记 - 姓名搜索
     * 失联人员的状态条件约束：是安帮人员、没有失联的、没有解除安置帮教的、司法所补录人员中已经录入服刑信息的、对于吸毒状态没有要求
     * @param key 姓名查询
     * @return List<Map < String,Object>>
     */
    @RequestMapping("/azbj/bjdxInfo/slrygl/toSldj/getBaseInfoList")
    public List<Map<String,Object>> getBaseInfoList(@RequestParam(required = false, defaultValue = "-1") String key) {
        if(StringUtils.equals(key,Constants.NumberStr.NUM_STR_FU_ONE)) {
            return null;
        }
        UserDTO user = userService.getAuthentication();
        String departId = user.getDepartid();
        int level = Integer.parseInt(user.getLevel());
        String orgid = user.getOrgid();
        return bjdxTuoguannfoService.getPrisonerList(key,departId,level,orgid);
    }

    /**
     * 安置帮教管理 - 帮教对象信息管理 - 失联人员管理 - 失联登记 - 保存/找回
     * @param data data
     * @return Result
     */
    @Log(type = "新增", value = "失联人员登记或找回，参数中有id为找回【更新】，没id为登记【新增】")
    @RequestMapping("/azbj/bjdxInfo/qbrygl/bjdxSldjSave")
    public Result bjdxSldjSave(@RequestParam(required = false, defaultValue = "-1") String data) {
        UserDTO user = userService.getAuthentication();
        String id = JsonUtil.getValueFromJsonstrByKey(data, "id");
        Date date = new Date();
        if(null == id) {
            data = JsonUtil.setObjectToJsonStr(data, "adduserid",user.getDepartid(),"createTime",date,"createUser",user.getAccount(),"uploadFlag",Constants.Number.NUM_ZERO,"updateUser",user.getAccount(),"updateTime",date);
        } else {
            data = JsonUtil.setObjectToJsonStr(data, "updateUser",user.getAccount(), "updateTime",date);
        }
        return bjdxTuoguannfoService.saveSldjBaseInfo(data);
    }
}